package br.com.lazyprogrammer.worldsoccermanager;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import br.com.lazyprogrammer.worldsoccermanager.network.Request;
import br.com.lazyprogrammer.worldsoccermanager.network.model.dao.UserDAO;
import br.com.lazyprogrammer.worldsoccermanager.system.message.Message;
import br.com.lazyprogrammer.worldsoccermanager.util.PoolData;

/**
 * Created by Andre on 29/08/2014.
 */
public class ConfirmAccountActivity extends Activity implements View.OnClickListener, Request.Callback {

    private UserDAO userDAO	= new UserDAO();

    private Button buttonConfirmAccount;
    private LinearLayout layoutLoading;
    private TextView labelWelcome;

    @Override
    public void onCancel(Class<?> executingClass) {
        layoutLoading.setVisibility(View.GONE);
    }

    @Override
    public void onStart(Class<?> executingClass) {
        layoutLoading.setVisibility(View.VISIBLE);
    }

    @Override
    public void onSuccess(Class<?> executingClass, Object result) {
        layoutLoading.setVisibility(View.GONE);
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void onError(Class<?> executingClass, Request.Response response, String errorMessage) {
        Message.send(this, errorMessage);
        layoutLoading.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View view) {
        if (view.equals(buttonConfirmAccount))
            confirmAccount();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setTitle(R.string.title_validate);
        setContentView(R.layout.activity_confirm_account);

        buttonConfirmAccount	= (Button) findViewById(R.id.button_validate);
        layoutLoading	= (LinearLayout) findViewById(R.id.layout_loading);
        labelWelcome	= (TextView) findViewById(R.id.label_welcome);

        labelWelcome.setText(getString(R.string.label_welcome, PoolData.user.getUsername()));

        buttonConfirmAccount.setOnClickListener(this);
    }

    private void confirmAccount() {
        if (!userDAO.isValidateTaskRunning())
            userDAO.validate(this, this, PoolData.user.getId(), PoolData.user.getKey());
    }
}
